package Controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hr-system/Global"
	"hr-system/Model"
	response "hr-system/Response"
	"time"
)

type IOtherInformationController interface {
	RestController
	Update(c *gin.Context)
	Delete(c *gin.Context)
}

type OtherInformationController struct {
	DB *gorm.DB
}


func NewOtherInformationController() IOtherInformationController {
	db := Global.GetDB()
	return OtherInformationController{DB: db}
}

func (o OtherInformationController) PageList(c *gin.Context) {
	var requestEmployee Model.Employee
	_ = c.ShouldBind(&requestEmployee)
	name := requestEmployee.Name
	if name != "" {
		var selectEmployee Model.Employee
		// 查询
		result := o.DB.Where("name=? and is_quit='否'", name).
			First(&selectEmployee).RowsAffected
		if result == 0 {
			response.Fail(c, "查无此员工", nil)
			return
		}
		var tmp Model.OtherInformation
		o.DB.Where("id=?",selectEmployee.Id).First(&tmp)
		re := make([]Model.APIOtherInformation, 0)
		re = append(re, Model.APIOtherInformation{
			Name:           selectEmployee.Name,
			UniInformation: tmp.UniInformation,
			OccupationStart: tmp.OccupationStart.Format("2006-01-02"),
			OccupationEnd: tmp.OccupationEnd.Format("2006-01-02"),
		})
		response.Success(c, gin.H{"data": re}, "查询成功")
		return
	}
	var record []Model.OtherInformation
	o.DB.Order("id").Find(&record)

	re := make([]Model.APIOtherInformation,0)
	length := len(record)
	for i := 0; i < length; i++ {
		var tmp Model.Employee
		have := o.DB.Where("id=? and is_quit='否'",record[i].Id).First(&tmp).RowsAffected
		if have == 0 { continue }
		row := Model.APIOtherInformation{
			Name: tmp.Name,
			UniInformation: record[i].UniInformation,
			OccupationStart: record[i].OccupationStart.Format("2006-01-02"),
			OccupationEnd: record[i].OccupationEnd.Format("2006-01-02"),
		}
		re = append(re, row)
	}
	response.Success(c, gin.H{"data": re}, "查询成功")
}

func (o OtherInformationController) Create(c *gin.Context) {
	var requestOtherInformation Model.APIOtherInformation
	_ = c.ShouldBind(&requestOtherInformation)
	selectOtherInformation := Model.OtherInformation{}
	result := o.DB.Where("id=?", requestOtherInformation.Id).
		Find(&selectOtherInformation).RowsAffected
	if result != 0 {
		response.Fail(c, "信息已存在", nil)
		return
	}
	start, _ := time.Parse("2006-01-02",requestOtherInformation.OccupationStart)
	end ,_ := time.Parse("2006-01-02",requestOtherInformation.OccupationEnd)
	newInformation := Model.OtherInformation{
		UniInformation: requestOtherInformation.UniInformation,
		OccupationStart: start,
		OccupationEnd: end,
	}
	err := o.DB.Create(&newInformation).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestOtherInformation}, "信息录入成功")
}

func (o OtherInformationController) Update(c *gin.Context) {
	var requestOtherInformation Model.APIOtherInformation
	_ = c.ShouldBind(&requestOtherInformation)
	start, _ := time.Parse("2006-01-02",requestOtherInformation.OccupationStart)
	end ,_ := time.Parse("2006-01-02",requestOtherInformation.OccupationEnd)
	updateOtherInformation := Model.OtherInformation{
		UniInformation: requestOtherInformation.UniInformation,
		OccupationStart: start,
		OccupationEnd: end,
	}
	err := o.DB.Model(&updateOtherInformation).
		Where("id=?", requestOtherInformation.Id).
		Updates(&updateOtherInformation).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestOtherInformation}, "修改成功")
}

func (o OtherInformationController) Delete(c *gin.Context) {
	var requestEmployee Model.Employee
	_ = c.ShouldBind(&requestEmployee)
	id := requestEmployee.Id
	err := o.DB.Where("id=?",id).Delete(&Model.OtherInformation{}).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, nil, "删除成功")
}
